package com.growthgate.bootcommon.common;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.jetbrains.annotations.NotNull;

/**
 * 响应返回
 *
 * @author GrowthGate
 * @date 2020/2/14
 */
@Data
@ApiModel
@Accessors(chain = true)
public class ResponseResult<T> {

    /**
     * 返回数据
     */
    @ApiModelProperty("返回数据")
    private T data;

    /**
     * 状态码
     */
    @ApiModelProperty("状态码")
    private int code;

    /**
     * 信息
     */
    @ApiModelProperty("信息")
    private String msg;

    public static <T> ResponseResult<T> success(T data) {
        ResponseMsg responseMsg = ResponseMsg.SUCCESS;
        return new ResponseResult<T>().setCode(responseMsg.getHttpStatus().value()).setMsg(responseMsg.getMsg()).setData(data);
    }

    public static <T> ResponseResult<T> fail(@NotNull ResponseMsg responseMsg) {
        return new ResponseResult<T>().setCode(responseMsg.getHttpStatus().value()).setMsg(responseMsg.getMsg());
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

}